#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i) 
#define mkp std::make_pair
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
inline bool cmp(int x,int y){return x < y;}

std::priority_queue< std::pair< ll , std::pair<int,int> > > q;
const int N = 1e6+9;
const ll inf = 1e12+9;
const double eps = 1e-7;
int _,n,m,a,b,c;
ll dis[1010][1010][4],map[1010][1010],ans = inf;
bool flag[1010][1010];

inline void dij(int x,int y,int id){
    rep(i,1,n) rep(j,1,n) flag[i][j] = false;
    dis[x][y][id] = map[x][y];
    while(q.size()) q.pop();
    q.push(mkp(-dis[x][y][id],mkp(x,y)));
    while(q.size()){
        std::pair<ll , std::pair<int,int> > now = q.top(); q.pop();

        if(flag[now.second.first][now.second.second]) continue;
        
        flag[now.second.first][now.second.second] = true;
        if(now.second.first > 1 && dis[now.second.first-1][now.second.second][id] > dis[now.second.first][now.second.second][id] + map[now.second.first-1][now.second.second]){
            dis[now.second.first-1][now.second.second][id] = dis[now.second.first][now.second.second][id] + map[now.second.first-1][now.second.second];
            q.push(mkp(-dis[now.second.first-1][now.second.second][id],mkp(now.second.first-1,now.second.second)));
        }
        if(now.second.first < n && dis[now.second.first+1][now.second.second][id] > dis[now.second.first][now.second.second][id] + map[now.second.first+1][now.second.second]){
            dis[now.second.first+1][now.second.second][id] = dis[now.second.first][now.second.second][id] + map[now.second.first+1][now.second.second];
            q.push(mkp(-dis[now.second.first+1][now.second.second][id],mkp(now.second.first+1,now.second.second)));
        }
        if(now.second.second > 1 && dis[now.second.first][now.second.second-1][id] > dis[now.second.first][now.second.second][id] + map[now.second.first][now.second.second-1]){
            dis[now.second.first][now.second.second-1][id] = dis[now.second.first][now.second.second][id] + map[now.second.first][now.second.second-1];
            q.push(mkp(-dis[now.second.first][now.second.second-1][id],mkp(now.second.first,now.second.second-1)));
        }
        if(now.second.second < n && dis[now.second.first][now.second.second+1][id] > dis[now.second.first][now.second.second][id] + map[now.second.first][now.second.second+1]){
            dis[now.second.first][now.second.second+1][id] = dis[now.second.first][now.second.second][id] + map[now.second.first][now.second.second+1];
            q.push(mkp(-dis[now.second.first][now.second.second+1][id],mkp(now.second.first,now.second.second+1)));
        }
    }
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef LOCAL //use "g++ xxx.cpp -DLOCAL"
        freopen("in.in", "r", stdin);
    #endif
    cin >> n >> m >> a >> b >> c;
    rep(i,1,n) rep(j,1,m) cin >> map[i][j];
    rep(i,1,n) rep(j,1,n) rep(k,1,3) dis[i][j][k] = inf;
    dij(1,a,1) , dij(n,b,2) , dij(n,c,3);
    rep(i,1,n) rep(j,1,n){
        ll sum = -2*map[i][j];
        rep(k,1,3) sum += dis[i][j][k];
        // cout << i << " " << j << " " << dis[i][j][1] << " " << dis[i][j][2] << " " << dis[i][j][3] << "\n";
        ans = std::min(ans,sum);
    }
    cout << ans << "\n";
    return 0;
}
